package FSMDE.models.tests;

import java.util.UUID;

import junit.framework.Assert;

import main.java.FSMDE.models.ComponentType;
import main.java.FSMDE.models.FactoryComponent;
import main.java.FSMDE.models.FactoryTransition;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.InterfaceGraphType;
import main.java.FSMDE.models.InterfaceType;
import main.java.FSMDE.models.Transition;
import main.java.FSMDE.models.TransitionControl;
import main.java.FSMDE.models.TransitionType;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class TransitionTestCase {

	private Interface firstInterface;
	private Interface secondInterface;

	@Test
	public void testCreatingTransitionSetsId() {
		Transition controlTransition = FactoryTransition
				.createControlTransition("TestName");
		Assert.assertTrue(controlTransition.getId() instanceof UUID);

		Transition funtionalTransition = FactoryTransition
				.createFunctionalTransition("TestName");
		Assert.assertTrue(funtionalTransition.getId() instanceof UUID);

		Assert.assertTrue(controlTransition.getId().toString() != funtionalTransition
				.getId().toString());
	}

	@Test
	public void testCreatingTransitionSetsTypes() throws Exception {
		Transition controlTransition = FactoryTransition
				.createControlTransition("TestName");
		Assert.assertSame(controlTransition.getType(), TransitionType.Control);

		Transition funtionalTransition = FactoryTransition
				.createFunctionalTransition("TestName");
		Assert.assertSame(funtionalTransition.getType(),
				TransitionType.Functional);
	}

	@Test
	public void testCreatingTransitionControl() throws Exception {
		TransitionControl controlTransition = FactoryTransition
				.createControlTransition("TestName");
		Assert.assertSame(controlTransition.getType(), TransitionType.Control);
		Assert.assertNull(controlTransition.getTriggerComponent());
	}

	@Test
	public void testCanCreateTransitionWithDifferentInterfaces()
			throws Exception {
		Transition transition = FactoryTransition
				.createControlTransition("TestName");
		transition.setSource(firstInterface);
		transition.setDestination(secondInterface);

		Assert.assertSame(firstInterface, transition.getSource());
		Assert.assertSame(secondInterface, transition.getDestination());
		Assert.assertNotSame(transition.getSource(),
				transition.getDestination());

		transition = FactoryTransition.createFunctionalTransition("TestName");
		transition.setSource(firstInterface);
		transition.setDestination(secondInterface);

		Assert.assertSame(firstInterface, transition.getSource());
		Assert.assertSame(secondInterface, transition.getDestination());
		Assert.assertNotSame(transition.getSource(),
				transition.getDestination());
	}

	@Test
	public void testCanSetTransitionWithSameInterfaceAsSourceAndDestination()
			throws Exception {
		Transition transition = FactoryTransition
				.createControlTransition("TestName");
		transition.setSource(firstInterface);
		transition.setDestination(firstInterface);

		Assert.assertSame(firstInterface, transition.getSource());
		Assert.assertSame(firstInterface, transition.getDestination());

		transition = FactoryTransition.createFunctionalTransition("TestName");
		transition.setSource(firstInterface);
		transition.setDestination(secondInterface);

		Assert.assertSame(firstInterface, transition.getSource());
		Assert.assertSame(secondInterface, transition.getDestination());
	}

	@Test
	public void testAddTriggerComponent() {
		TransitionControl transition = FactoryTransition
				.createControlTransition("TestName");
		Assert.assertNull(transition.getTriggerComponent());

		main.java.FSMDE.models.Component boton = FactoryComponent.create("TextName",
				ComponentType.Button);
		transition.setTriggerComponent(boton);
		Assert.assertEquals(boton, transition.getTriggerComponent());
	}

	@Before
	public void setUp() throws Exception {
		this.firstInterface = new Interface("RelatedInterface1",
				InterfaceType.General, InterfaceGraphType.Normal);
		this.secondInterface = new Interface("RelatedInterface2",
				InterfaceType.General, InterfaceGraphType.Normal);
	}

	@After
	public void tearDown() throws Exception {
	}
}
